<?php
class JobsController extends AppController {

	var $name = 'Jobs';
    var $uses = array('City','Province','Job','JobType','EmployType','Comment','Language');
    var $helpers = array('Formater','Linker','Avatar');


	function index() {
		$this->Job->recursive = 0;
		$this->set('jobs', $this->paginate('Job'));
        $provinces = $this->Province->find('all');
        $employ_types = $this->Job->EmployType->find('list');
		$job_types = $this->Job->JobType->find('list');
		$languages = $this->Language->find('list');
		$this->set(compact('languages') );
		$this->set(compact('employ_types'));
		$this->set(compact('job_types'));
        $this->set('provinces', $provinces);
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid job', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->paginate=array(
		    'order'=>'Comment.created desc'
        		);
		$comments=$this->paginate('Comment',array('Comment.commentable_type'=>'Job','Comment.commentable_id'=>$id));
		$this->set('comments', $comments);
		$this->set('job', $this->Job->read(null, $id));
	}

	function search()
	{
        $provinces = $this->Province->find('all');
        $employ_types = $this->Job->EmployType->find('list');
		$job_types = $this->Job->JobType->find('list');
		$languages = $this->Language->find('list');
		$this->set(compact('languages') );
		$this->set(compact('employ_types'));
		$this->set(compact('job_types'));
        $this->set('provinces', $provinces);
		$conditions = array();
		$_params = $this->params['url'];
		if (isset($_params['city_id']) && !empty($_params['city_id'])) {
			$conditions["Job.city_id"]=$_params['city_id'];
		}
		if (!empty($_params['l_id'])) {
			$conditions['Job.language_id']=$_params['l_id'];
		}
		if (!empty($_params['et_id'])) {
			$conditions['Job.employ_type_id']=$_params['et_id'];
		}
		if(!empty($_params['jt_id'])){
			$conditions['Job.job_type_id']=$_params['jt_id'];
		}

		$jobs = $this->Paginate('Job',$conditions);
		$this->set('jobs', $jobs);
	}
	function add() {
		if (!empty($this->data)) {
			$this->Job->create();
			if ($this->Job->save($this->data)) {
				$this->Session->setFlash(__('The job has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The job could not be saved. Please, try again.', true));
			}
		}
        $provinces = $this->Province->find('all');
        $employ_types = $this->Job->EmployType->find('list');
		$job_types = $this->Job->JobType->find('list');
		$languages = $this->Language->find('list');
		$this->set(compact('languages') );
		$this->set(compact('employ_types'));
		$this->set(compact('job_types'));
        $this->set('provinces', $provinces);
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid job', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
		    $this->Job->id = $id;
			if ($this->Job->save($this->data)) {
				$this->Session->setFlash(__('The job has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The job could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Job->read(null, $id);
		}
        $provinces = $this->Province->find('all');
        $employ_types = $this->Job->EmployType->find('list');
		$job_types = $this->Job->JobType->find('list');
		$languages = $this->Language->find('list');
		$this->set(compact('languages') );
		$this->set(compact('employ_types'));
		$this->set(compact('job_types'));
        $this->set('provinces', $provinces);
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for job', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Job->delete($id)) {
			$this->Session->setFlash(__('Job deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Job was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
?>